/*
 * Copyright (c) 2005 David HENRY
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef _MDL_H_
#define _MDL_H_

#include "../../mgpi_stddefs.h"

#define IDPO 1330660425

typedef float vec3_t[3];	/* vector */

#pragma pack(push, 1)

typedef struct {
	int ident;            	/* magic number: "IDPO" */
  	int version;          	/* version: 6 */

  	vec3_t scale;         	/* scale factor */
  	vec3_t translate;     	/* translation vector */
  	float boundingradius;
  	position_t eyeposition; /* eyes' position */

  	int num_skins;        	/* number of textures */
  	int skinwidth;        	/* texture width */
  	int skinheight;       	/* texture height */

  	int num_verts;        	/* number of vertices */
  	int num_tris;         	/* number of triangles */
  	int num_frames;       	/* number of frames */

  	int synctype;         	/* 0 = synchron, 1 = random */
  	int flags;            	/* state flag */
  	float size;
	} mdl_header_t;		/* mdl header */

typedef struct {
  	int group;      	/* 0 = single, 1 = group */
  	GLubyte *data;  	/* texture data */
	} mdl_skin_t;		/* skin */

typedef struct {
	int onseam;
	int s;
  	int t;
	} mdl_texCoord_t;	/* texture coords */

typedef struct {
  	int facesfront;  	/* 0 = backface, 1 = frontface */
  	int vertex[3];   	/* vertex indices */
	} mdl_triangle_t;	/* triangle info */

typedef struct {
  	UCHAR v[3];
  	UCHAR normalIndex;
	} mdl_vertex_t;		/* compressed vertex */

typedef struct {
  	mdl_vertex_t bboxmin;	/* bouding box min */
  	mdl_vertex_t bboxmax;	/* bouding box max */
  	char name[16];
  	mdl_vertex_t *verts;  	/* vertex list of the frame */
	} mdl_simpleframe_t;	/* simple frame */

typedef struct {
  	int type;               /* 0 = simple, !0 = group */
  	mdl_simpleframe_t frame;
	} mdl_frame_t;		/* model frame */

typedef struct {
  	mdl_header_t header;

  	mdl_skin_t *skins;
  	mdl_texCoord_t *texcoords;
  	mdl_triangle_t *triangles;
  	mdl_frame_t *frames;

  	GLuint *tex_id;
  	int iskin;

	void *vparam;
	} mdl_model_t;		/* mdl model structure */

#pragma pack(pop)

GLuint makeTextureMDL( int , mdl_model_t * );
int readMDLModel( const char * , mdl_model_t * );
void freeMDLModel( mdl_model_t * );
void renderMDLFrame( int , mdl_model_t * );
void renderMDLFrameItp( int , float , mdl_model_t * );
void animateMDL( int , int , int * , float * );

#endif

